<?php
namespace app\api\controller;
use think\Db;
use think\Image;
use think\Controller;
use think\Request;
class Find extends Controller
{
  public function AjaxReturn($code,$msg,$data=array())
	{

		$returnData=array(
			"code"=>$code
		);
		if($msg)
		{
		  $returnData["message"]=$msg;
		}
		if(!empty($data))
		{
			$returnData["data"]=$data;
		}
		exit(json_encode($returnData));
	}
   public function chakan($openid){
   		$id=db('user')->where('code',$openid)->value('uid');
        return $id;
   }
	
 
	
	  	//找公司搜索
	public function gongSearch(){
      $openid=$this->chakan(input('openid'));
		if(input('name')!=""){
          $page=input('page');
           $where ['uflg'] = 1;
			$name=input('name');
			$rs=db("user")
			->join('f_company','f_user.uid=f_company.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_real.uid')
            ->where($where)
			->where('company','like','%'.$name.'%')
			->whereor('product','like','%'.$name.'%')
            ->order("f_user.createtime DESC")
            ->page($page,10)
			->select();
          	for($i=0;$i<count($rs);$i++){
            	if($openid == $rs[$i]['uid']){
                	unset($rs[$i]);
                  	break;
                }
            }
           $rs=array_values($rs);
           foreach($rs as  $key1=>$val1){ 
                $fuid=array_filter(explode(',',$val1['fuid']));
                  if(in_array($openid,$fuid))
                  {
                  		$rs[$key1]['fuid']='1';
                  }
                  else
                  {
                        $rs[$key1]['fuid']='0';
                  }
                 
                  if($rs[$key1]['uname']=='')
                  {
                   $rs[$key1]['nickname']=$rs[$key1]['nickname'];
                  }
                 else
                 {
                   $rs[$key1]['nickname']=$rs[$key1]['uname'];
                 }
               }  
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
			
		}else{
        	return $this->AjaxReturn('-2','暂无信息');
        }
	}
  //找老板搜索
	public function bosSearch(){
      $openid=$this->chakan(input('openid'));
		if(input('name')!=""){
          $page=input('page');
            $where ['uflg'] = 1;
			$name=input('name');
			$rs=db("user")
			->join('f_company','f_user.uid=f_company.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_real.uid')
            ->where($where)
			->where('uname','like','%'.$name.'%')
            ->whereor('nickname','like','%'.$name.'%')
			->whereor('product','like','%'.$name.'%')
            ->whereor('phone','like','%'.$name.'%')
            ->order("f_user.createtime DESC")
            ->page($page,10)
			->select();
          	for($i=0;$i<count($rs);$i++){
            	if($openid == $rs[$i]['uid']){
                	unset($rs[$i]);
                  	break;
                }
            }
             $rs=array_values($rs);
             foreach($rs as  $key1=>$val1){
                $fuid=array_filter(explode(',',$val1['fuid']));
                  if(in_array($openid,$fuid))
                  {
                  		$rs[$key1]['fuid']='1';
                  }
                  else
                  {
                        $rs[$key1]['fuid']='0';
                  }
                  if($rs[$key1]['uname']=='')
                  {
                   $rs[$key1]['nickname']=$rs[$key1]['nickname'];
                  }
                 else
                 {
                   $rs[$key1]['nickname']=$rs[$key1]['uname'];
                 }
               }
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
		}
		else{
			return $this->AjaxReturn('-1','暂无信息');
		}   
	}
}